home *** CD-ROM | disk | FTP | other *** search
/ Aminet 15 / Aminet 15 - Nov 1996.iso / Aminet / dev / lang / FPL_v147.lha / fpl / src / caller.c next >
C/C++ Source or Header  |  1996-08-09  |  13KB  |  463 lines

  1. /******************************************************************************
  2.  *                   FREXX PROGRAMMING LANGUAGE                  *
  3.  ******************************************************************************
  4.  
  5.  caller.c
  6.  
  7.  For FPL debugging...
  8.  Only part of the executable file version of FPL.
  9.  
  10.  *****************************************************************************/
  11.  
  12. /************************************************************************
  13.  *                                                                      *
  14.  * fpl.library - A shared library interpreting script langauge.         *
  15.  * Copyright (C) 1992-1995 FrexxWare                                    *
  16.  * Author: Daniel Stenberg                                              *
  17.  *                                                                      *
  18.  * This program is free software; you may redistribute for non          *
  19.  * commercial purposes only. Commercial programs must have a written    *
  20.  * permission from the author to use FPL. FPL is *NOT* public domain!   *
  21.  * Any provided source code is only for reference and for assurance     *
  22.  * that users should be able to compile FPL on any operating system     *
  23.  * he/she wants to use it in!                                           *
  24.  *                                                                      *
  25.  * You may not change, resource, patch files or in any way reverse      *
  26.  * engineer anything in the FPL package.                                *
  27.  *                                                                      *
  28.  * This program is distributed in the hope that it will be useful,      *
  29.  * but WITHOUT ANY WARRANTY; without even the implied warranty of       *
  30.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.                 *
  31.  *                                                                      *
  32.  * Daniel Stenberg                                                      *
  33.  * Ankdammsgatan 36, 4tr                                                *
  34.  * S-171 43 Solna                                                       *
  35.  * Sweden                                                               *
  36.  *                                                                      *
  37.  * FidoNet 2:201/328    email:dast@sth.frontec.se                       *
  38.  *                                                                      *
  39.  ************************************************************************/
  40.  
  41.  
  42. #include "FPL.h"
  43. #include "reference.h"        /* should be <FPL/reference.h> */
  44.  
  45. #include "config.h"      /* system dependent setups */
  46.  
  47. #ifdef AMIGA
  48. #include <exec/types.h>
  49. #include <proto/exec.h>
  50.  
  51. int CXBRK(void) { return(0); }  /* Disable Lattice/SAS CTRL/C handling */
  52. int chkabort(void) { return(0); }  /* really */
  53. #ifdef SHARED
  54. #include <exec/libraries.h>
  55. #include <libraries/dos.h>
  56.  
  57. #include "/include/pragmas/FPL_pragmas.h"
  58. #include "/include/clib/FPL_protos.h"
  59. struct Library *FPLBase = NULL;
  60. #endif
  61.  
  62. #define REG(x) register __ ## x
  63.  
  64. #else
  65. #include <sys/types.h>
  66.  
  67. #define REG(x)
  68. #define TRUE  1
  69. #define FALSE 0
  70. #include "../include/clib/FPL_protos.h"
  71.  
  72. #endif
  73.  
  74. #include <stdlib.h>
  75. #include <string.h>
  76. #include <stdio.h>
  77.  
  78. #if defined(AMIGA) && defined(SHARED)
  79. #define CALLER __saveds
  80. #define ASM __asm
  81. #else
  82. #define CALLER
  83. #define ASM
  84. #endif
  85.  
  86. long ASM func(REG(a0) struct fplArgument *);
  87. long ASM inter(REG(a0) void *);
  88. void CALLER ASM MyFree(REG(a1) void *, REG(d0) long);
  89. void CALLER ASM *MyAlloc(REG(d0) long);
  90. long ASM newline(REG(a0) void *);
  91.  
  92. int memory_counter=0;
  93. int maxmemory_counter=0;
  94. int mallocs=0;
  95.  
  96. int newlines=0;
  97.  
  98. enum myfunctions {
  99.   FN_GETINT,
  100.   FN_GETSTRING,
  101.   FN_OUTPUT,
  102.   FN_EXECUTE,
  103.   FN_PRINTF,
  104.   FN_TEST,
  105.   FN_OPENL,
  106.   FN_CLOSEL,
  107.  
  108.   VR_TEST /* first variable ever! */
  109.  
  110.   };
  111.  
  112.  
  113.  
  114. /**********************************************************************
  115.  *
  116.  * int main(int, char **)
  117.  *
  118.  * This function is not included in the run time library version.
  119.  *
  120.  ******/
  121.  
  122. void *key;
  123.  
  124. int main(int argc, char **argv)
  125. {
  126.   long n, end=0;
  127.   long count=0;
  128.   int pre_mallocs;
  129.   long pre_malloc;
  130.   struct fplSymbol *symbols;
  131.   long i;
  132.   long Version_I,Revision_I;
  133.  
  134. #if defined(SHARED) && defined(AMIGA)
  135.   if(!(FPLBase=OpenLibrary(FPLNAME, 5))) {
  136.     printf("Error opening %s!\n", FPLNAME);
  137.     return(-1);
  138.   }
  139. #endif
  140.  
  141.   if(argc<2) {
  142.     printf("Usage: SFPL <FPL program file name>\n");
  143. #if defined(AMIGA) && defined(SHARED)
  144.     CloseLibrary((struct Library *)FPLBase);
  145. #endif
  146.     return 0;
  147.   }
  148.  
  149.   key=fplInitTags(func,
  150. #if 0
  151.           FPLTAG_INTERVAL, (unsigned long)inter, /* interval func */
  152.           FPLTAG_USERDATA, (unsigned long)&count, /* user data */
  153.           FPLTAG_INTERNAL_DEALLOC, (unsigned long)MyFree,
  154.           FPLTAG_INTERNAL_ALLOC, (unsigned long)MyAlloc,
  155.           FPLTAG_MINSTACK, 7000,
  156. #endif
  157.           FPLTAG_CACHEALLFILES, FPLCACHE_EXPORTS,
  158.                   FPLTAG_REREAD_CHANGES, TRUE,
  159. #if 0
  160.                   FPLTAG_IDENTITY, argv[0], /* identify us! */
  161.           FPLTAG_DEBUG, TRUE,  /* run in debug mode! */
  162. /*                  FPLTAG_AUTOCOMPILE, TRUE, */
  163. #endif
  164.           FPLTAG_DONE);
  165.  
  166.   fplSendTags( key, FPLSEND_GETVERSION, &Version_I,
  167.                     FPLSEND_GETREVISION, &Revision_I, FPLSEND_DONE);
  168.   printf("Using FPL version %d.%d.\n",Version_I,Revision_I);
  169.   
  170.   fplAddFunction(key, "getint",    FN_GETINT,    'I', "s", NULL);
  171.   fplAddFunction(key, "getstring", FN_GETSTRING, 'S', "s", NULL);
  172.   fplAddFunction(key, "output",       FN_OUTPUT,    'S', "O", NULL);
  173.   fplAddFunction(key, "runfile",   FN_EXECUTE,   'I', "S", NULL);
  174.   fplAddFunction(key, "printf",    FN_PRINTF,    'I', "So>", NULL);
  175.   fplAddFunction(key, "array",     FN_TEST,      'I', "R", NULL);  
  176.  
  177.   fplAddVariable(key, "_TEST_",    VR_TEST,      'S', NULL, NULL);
  178.  
  179.   pre_mallocs=mallocs;
  180.   pre_malloc=memory_counter;
  181.  
  182. #if 1
  183.   if(!end && argc>1) {
  184.     int i;
  185.     for(i=1; i<argc; i++) {
  186.       char *string=NULL;
  187.       end=fplExecuteFileTags(key, argv[i],
  188.                              /* FPLTAG_ISOLATE, TRUE, */
  189.                              FPLTAG_STRING_RETURN, &string,
  190.                              FPLTAG_DONE);
  191.       if(string) {
  192.         printf("The '%s' program returned '%s'\n", argv[i], string);
  193.         fplFreeString(key, string);
  194.       }
  195.     }
  196.   }
  197. #else
  198.   {
  199.     char *program1 =
  200.       "int export amiga()\n{ Request(\"hej\"); }";
  201.     int a;
  202.     for(a=0; a<100; a++) {
  203.       fplExecuteScriptTags(key, &program1, 1,
  204.                            FPLTAG_KIDNAP_CACHED, TRUE,
  205.                            FPLTAG_CACHEFILE, FPLCACHE_EXPORTS,
  206.                            FPLTAG_PROGNAME, "program1",
  207.                            FPLTAG_DONE);
  208.       fplSendTags(key, FPLSEND_FREEFILE, (long)"program1",
  209.                   FPLTAG_DONE);
  210.     }
  211.   }
  212. #endif
  213.  
  214. #if 0  
  215.   fplSendTags(key, FPLSEND_GETSYMBOL_FUNCTIONS, &symbols, FPLSEND_DONE);
  216.   printf("\n---------------------\nAll exported functions:\n");
  217.   for(i=0; i<symbols->num; i++)
  218.     printf("%s ", symbols->array[i]);
  219.   fplSendTags(key, FPLSEND_GETSYMBOL_FREE, symbols, FPLSEND_DONE);
  220.  
  221.   fplSendTags(key, FPLSEND_GETSYMBOL_VARIABLES, &symbols, FPLSEND_DONE);
  222.   printf("\n---------------------\nAll exported variables:\n");
  223.   for(i=0; i<symbols->num; i++)
  224.     printf("%s ", symbols->array[i]);
  225.   fplSendTags(key, FPLSEND_GETSYMBOL_FREE, symbols, FPLSEND_DONE);
  226.  
  227.   fplSendTags(key, FPLSEND_GETSYMBOL_CACHEDFILES, &symbols, FPLSEND_DONE);
  228.   printf("\n---------------------\nAll cached files:\n");
  229.   for(i=0; i<symbols->num; i++)
  230.     printf("%s ", symbols->array[i]);
  231.   fplSendTags(key, FPLSEND_GETSYMBOL_FREE, symbols, FPLSEND_DONE);
  232. #endif
  233.  
  234.   fplSendTags(key,
  235.           FPLSEND_GETRETURNCODE, &n,
  236.           FPLSEND_FLUSHFILE, 0,
  237.           FPLSEND_FLUSHCACHE, 1,
  238.           FPLSEND_DONE);
  239.  
  240.   fplFree(key); /* free all shit FPL uses internally */
  241.  
  242. #if defined(AMIGA) && defined(SHARED)
  243.   CloseLibrary((struct Library *)FPLBase);
  244. #endif
  245.  
  246. #ifdef DEBUG_INFO  
  247.   printf("\n-----------------------------------------\n");
  248.   printf("Return code   :  %d\n", n);
  249.   printf("Interval func :  %d\n", count);
  250.   printf("Newlines      :  %d\n", newlines);
  251.   printf("Pre mallocs   :  %d\n", pre_mallocs);
  252.   printf("Pre memory use:  %d\n", pre_malloc);
  253.   printf("Malloc        :  %d\n", mallocs-pre_mallocs);
  254.   printf("memory use    :  %d\n", maxmemory_counter-pre_malloc);
  255.   printf("-----------------------------------------\n");
  256. #endif
  257.  
  258.   if(memory_counter)
  259.     printf(">ALARM!!< Not freed mem :  %d bytes!\n", memory_counter);
  260.  
  261.   return end;
  262. }
  263.  
  264.  
  265.  
  266. #define TEST_STRING "I_returned_this_from_the_interface_function!"
  267.  
  268. long ASM func(REG(a0) struct fplArgument *arg)
  269. {
  270.   int ret;
  271.   long col;
  272.   char *name;
  273.   char *string;
  274.   void *anchor=arg->key;
  275.   char systemline[80];
  276.   switch(arg->ID) {
  277.   case VR_TEST:
  278.     fplSendTags(anchor, FPLSEND_STRING, "internal variable!", FPLSEND_DONE);
  279.     break;
  280.   case FN_TEST:
  281.     {
  282.       long new[2]={0,-1};
  283.       long *integer;
  284.       fplReferenceTags(anchor, arg->argv[0],
  285.                        FPLREF_NAME, &name,
  286.                FPLREF_TYPE, &col,
  287.                FPLREF_GET_STRING, &string,
  288.                FPLREF_GET_INTEGER, &integer,
  289.                FPLREF_DONE);
  290.       printf("Received a reference to the %s %svariable called '%s'\n",
  291.              col&FPLREF_TYPE_STRING?"string":"integer",
  292.              col&FPLREF_TYPE_ARRAY?"array ":"",
  293.              name);
  294.  
  295.       if(col&FPLREF_TYPE_ARRAY) {
  296.     struct fplRef ref;
  297.     fplReferenceTags(anchor, arg->argv[0],
  298.              FPLREF_ARRAY_INFO, &ref,
  299.              FPLREF_DONE);
  300.     if(1 == ref.Dimensions) {
  301.       long dims[]={0, -1};
  302.       for(col=0; col<ref.ArraySize[0]; col++) {
  303.         dims[0]=col;
  304.         fplReferenceTags(anchor, arg->argv[0],
  305.                  FPLREF_ARRAY_ITEM, &dims[0],
  306.                  FPLREF_GET_STRING, &name,
  307.                  FPLREF_DONE);
  308.         if(name[0])
  309.           printf("#%d: %s\n", col, name);
  310.       }
  311.       new[0] = 5;
  312.       fplReferenceTags(anchor, arg->argv[0],
  313.                FPLREF_ARRAY_ITEM, &dims[0],
  314.                FPLREF_SET_MY_STRING, "externally set",
  315.                FPLREF_DONE);
  316.           ref.ArraySize = new;
  317.           new[0] = 20;
  318.       fplReferenceTags(anchor, arg->argv[0],
  319.                            FPLREF_ARRAY_RESIZE, &ref,
  320.                FPLREF_DONE);
  321.     }
  322.       }
  323.       else if(col&FPLREF_TYPE_STRING) {
  324.         printf("It holds the string '%s'\n", string);
  325.     if(string=(char *)fplAllocString(anchor, strlen(TEST_STRING))) {
  326.           strcpy(string, TEST_STRING);
  327.       fplReferenceTags(anchor, arg->argv[0],
  328.                        FPLREF_SET_STRING, string,
  329.                    FPLREF_END);
  330.       fplReferenceTags(anchor, arg->argv[0],
  331.                        FPLREF_SET_MY_STRING, TEST_STRING,
  332.                    FPLREF_END);
  333.     }
  334.       } else if(col&FPLREF_TYPE_INTEGER) {
  335.         printf("It holds the number %d\n", *integer);      
  336.       }
  337.     }
  338.     break;
  339.  
  340.   case FN_PRINTF:
  341. #if defined(AMIGA)
  342.     vprintf(arg->argv[0], (char *)&arg->argv[1]);
  343. #elif defined(UNIX)
  344.     vfprintf(stderr, arg->argv[0], (char *)&arg->argv[1]);
  345. #endif
  346.     break;
  347.   case FN_OUTPUT: /* output */
  348.     if(arg->format[0]==FPL_STRARG)  /* we got a string! */
  349.       string="%s";
  350.     else
  351.       string="%d";
  352. #if defined(AMIGA)
  353.     printf(string, arg->argv[0]);
  354. #elif defined(UNIX)
  355.     fprintf(stderr, string, arg->argv[0]);
  356. #endif
  357.  
  358.     fplSendTags(arg->key,
  359.                 FPLSEND_STRING, "returned",
  360.         FPLSEND_DONE);
  361.     break;
  362.     
  363.   case FN_GETSTRING:
  364.     if(string=(char *)fplAlloc(anchor, 64)) {
  365.       if(arg->argc)
  366.     printf("%s", arg->argv[0]);
  367.       fgets(string, 64, stdin);
  368.       ret=fplSendTags(arg->key,
  369.               FPLSEND_STRING, string,
  370.               FPLSEND_STRLEN, strlen(string)-1,
  371.               FPLSEND_DONE);
  372.       fplDealloc(anchor, string);
  373.     } else
  374.       ret=FPLERR_OUT_OF_MEMORY;
  375.     return(ret);
  376.  
  377.   case FN_EXECUTE:
  378.     ret=fplExecuteFile(anchor, arg->argv[0], NULL);
  379.     return(ret);
  380.     break;
  381.  
  382.   case FPL_GENERAL_ERROR:
  383.     {
  384.       char buffer[FPL_ERRORMSG_LENGTH];
  385.       fplSendTags(anchor,
  386.           FPLSEND_GETVIRLINE, &col,
  387.           FPLSEND_GETVIRFILE, &name,
  388.           FPLSEND_DONE);
  389.       if(*name=='\"') {
  390.     ret=0;
  391.     name++;
  392.     while(name[ret] && name[ret]!='\"')
  393.       ret++;
  394.     string=(char *)fplAlloca(anchor, ret+1);
  395.     memcpy(string, name, ret);
  396.     string[ret]='\0';
  397.       } else {
  398.     string=name;
  399.     ret=0;
  400.       }
  401.       printf("\n>>> %s\n",
  402.          fplGetErrorMsg(arg->key, (long)arg->argv[0], buffer));
  403.       printf(">>> Line %d in file \"%s\". <<<\n", col, string);
  404.       if(ret)
  405.     fplDealloca(anchor, string);
  406.     }
  407.     break;
  408.  
  409.   case FPL_UNKNOWN_FUNCTION:
  410.     col=22; /* only to breakpoint */
  411.     break;
  412.  
  413.   case FN_GETINT:
  414.     if(arg->argc)
  415.       printf("%s", (char *)arg->argv[0]);
  416.     scanf("%d", &ret);
  417.     ret=fplSendTags(anchor, FPLSEND_INT, ret, FPLSEND_DONE);
  418.     return(ret);
  419.   }
  420.   return FPL_OK;
  421. }
  422.  
  423. long ASM inter(REG(a0) void *count)
  424. {
  425.   static line=-1;
  426. #if 0
  427.   static char *name;
  428.   long current_line;
  429.   char *curr_name;
  430.  
  431.   fplSendTags(key, FPLSEND_GETVIRLINE, ¤t_line, FPLSEND_DONE);
  432.   fplSendTags(key, FPLSEND_GETVIRFILE, &curr_name, FPLSEND_DONE);
  433.   if(line!=current_line || name != curr_name) {
  434.     line=current_line;
  435.     name=curr_name;
  436. /*    fprintf(stderr, " < %d %s > ", line, name?name:"unknwon"); */
  437.   }
  438. #endif
  439.  
  440.   (*(int *)count)++; /* just to count the number of times this routine has been
  441.             called. */
  442.   return(0);
  443. }
  444.  
  445. void CALLER ASM MyFree(REG(a1) void *pntr, REG(d0) long size)
  446. {
  447.   memory_counter-=size;
  448.   memset(pntr, 0xaa, size); /* mess up this area before free! */
  449.   free(pntr);
  450. }
  451.  
  452. void CALLER ASM *MyAlloc(REG(d0) long size)
  453. {
  454.   void *mem;
  455.   mallocs++;
  456.   if((memory_counter+=size)>maxmemory_counter)
  457.     maxmemory_counter=memory_counter;
  458.   mem=malloc(size);
  459.   if(mem)
  460.     memset(mem, 0xaa, size); /* mess up this area before free! */
  461.   return (mem);
  462. }
  463.